package com.example.tms.mapper;

import com.example.tms.bean.CarInfo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 车辆信息
 */
@Repository
public interface CarMapper extends JpaRepository<CarInfo,String>, JpaSpecificationExecutor<CarInfo> {

    //查询车辆信息（车辆id）
    @Query(value = "select * from car_info where clid = ?1 and sfcz = true", nativeQuery = true)
    CarInfo findByClid(String clid);

    //查询车辆信息（部门id）
    @Query(value = "select * from car_info where bumenid = ?1 and sfcz = true", nativeQuery = true)
    List<CarInfo> findByBumenid(String bumenid);

    //查询车辆信息（司机id）
    @Query(value = "select * from car_info where sjid = ?1 and sfcz = true", nativeQuery = true)
    List<CarInfo> findBySjid(String sjid);

    //查询车辆
    @Query(value = "select * from car_info where clid = ?1 and sfcz = true and sffp = '已分配'", nativeQuery = true)
    CarInfo findSffpByClid(String clid);

    List<CarInfo> findBySfczOrderByLasttimeAsc(Boolean sfcz);

    //删除车辆信息（假删除 车辆id）
    @Query(value = "update car_info set sfcz = false where clid = ?1", nativeQuery = true)
    @Modifying
    @Transactional
    void DeleteByClid(String clid);

    CarInfo findByCphAndSfcz(String cph, boolean sfcz);

    //更新车辆状态（车辆id）
    @Query(value = "update car_info set clzt = ?2 where clid = ?1", nativeQuery = true)
    @Modifying
    @Transactional
    void updateCarZt(String clid, String clzt);

    //更新车辆状态
    @Query(value = "update car_info set sffp = ?2,clzt='' where clid = ?1", nativeQuery = true)
    @Modifying
    @Transactional
    void updateCarfp(String clid,String sffp);

    //更新车辆状态
    @Query(value = "update car_info set sffp = ?3,clzt=?2 where clid = ?1", nativeQuery = true)
    @Modifying
    @Transactional
    void updateBackCarfp(String clid ,String clzt,String sffp);

    //车辆添加驾驶员（车辆id）
    @Query(value = "update car_info set sjid = ?2 where clid = ?1", nativeQuery = true)
    @Modifying
    @Transactional
    void updateCarSj(String clid, String sjid);

    //车辆添加油卡（车辆id）
    @Query(value = "update car_info set ykid = ?2 where clid = ?1", nativeQuery = true)
    @Modifying
    @Transactional
    void updateCarYk(String clid, String ykid);

    //查询车辆信息（车牌号）
    CarInfo findByCph(String cph);

}